package com.game.common.utils;

import org.jasypt.util.text.BasicTextEncryptor;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

public class OrderNumberUtil {

    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    public static void main(String[] args) {
        pwd();
        //System.out.println(randomString6());
    }

    /**
     * 创建不连续的订单号
     *
     * @param no 数据中心编号
     * @return 唯一的、不连续订单号
     */
    public static synchronized String getOrderNoByUUID(String no) {
        Integer uuidHashCode = UUID.randomUUID().toString().hashCode();
        if (uuidHashCode < 0) {
            uuidHashCode = uuidHashCode * (-1);
        }
        String date = simpleDateFormat.format(new Date());
        return no + date + uuidHashCode;
    }

    /**
     * 　　* 把数组所有元素排序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
     * 　　* @param params 需要排序并参与字符拼接的参数组
     * 　　* @return 拼接后字符串
     * 　　* @throws UnsupportedEncodingException
     */
    public static String toParams(Map<String, String> params) {
        try {
            List<String> keys = new ArrayList<String>(params.keySet());
            Collections.sort(keys);
            String prestr = "";
            for (int i = 0; i < keys.size(); i++) {
                String key = keys.get(i);
                String value = params.get(key);
                value = URLEncoder.encode(value, "UTF-8");
                if (i == keys.size() - 1) {// 拼接时，不包括最后一个&字符
                    prestr = prestr + key + "=" + value;
                } else {
                    prestr = prestr + key + "=" + value + "&";
                }
            }
            return prestr;
        } catch (Exception e) {

        }
        return null;
    }

    public static String randomString6() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 6);
    }

    public static void pwd() {
        // 创建加密对象，默认 PBEWithMD5AndDES
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        // 加密所需的密钥
        textEncryptor.setPassword("shijianren189");
        // 加密后
        //String url = textEncryptor.encrypt("jdbc:mysql://47.251.8.43:3306/shopping?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2b8");
        String url = textEncryptor.encrypt("jdbc:mysql://47.251.8.43:3306/shopping?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2b8");
        String userName = textEncryptor.encrypt("root");
        String pwd = textEncryptor.encrypt("!Yi123456789");
        String redisIp = textEncryptor.encrypt("47.251.8.43");
        String redisPwd = textEncryptor.encrypt("reids123456");
        System.out.println("url:\n" + url);
        System.out.println("userName:\n" + userName);
        System.out.println("pwd:\n" + pwd);
        System.out.println("redisIp:\n" + redisIp);
        System.out.println("redisPwd:\n" + redisPwd);
        System.out.println(textEncryptor.decrypt("j3o0pcbs+f2R6kPB1jwVLqajP/U4E1CumQq85CrZvGYxoz3vLlkeIpu6/0cj/IdViHru6fK/UE2PxAoBIvtDoaANkPG4FVov5rwx204GwGUr6HoMuZTp7it1bFIfR9xUd+uqdiJmEXFk3IxUCoAuUZdv79u6+mWha6i1+ufllPAkvJ9IhBSQow=="));

    }
    /**
     * 生成随机的token
     *
     * @return token
     */
    public static String getRandomToken() {
        Random random = new Random();
        StringBuilder randomStr = new StringBuilder();

        // 根据length生成相应长度的随机字符串
        for (int i = 0; i < 32; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";

            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                randomStr.append((char) (random.nextInt(26) + temp));
            } else {
                randomStr.append(random.nextInt(10));
            }
        }

        return randomStr.toString();
    }
}
